package com.ship.system.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.common.core.domain.R;
import com.ship.system.api.domain.SysUser;
import com.ship.system.api.model.LoginUser;
import com.ship.system.domain.SysUserDept;
import com.ship.system.mapper.SysUserMapper;
import com.ship.system.service.ISysDeptService;
import com.ship.system.service.ISysMenuService;
import com.ship.system.service.ISysUserDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@Validated
public class RemoteUserServiceImpl implements RemoteUserService{

    @Resource
    private SysUserMapper userMapper;

    @Resource
    ISysUserDeptService userDeptService;

    @Resource
    ISysMenuService menuService;
    @Override
    public R<LoginUser> getUserInfo(String username, String source) {
        SysUser sysUser = userMapper.selectUserByUserName(username);
        // 岗位集合
        List<SysUserDept> deptList = userDeptService.getBaseMapper().selectList(
                new LambdaQueryWrapper<SysUserDept>()
                        .eq(SysUserDept::getUserId,sysUser.getUserId())
        );
        List<Long> depts = new LinkedList<>();
        if(deptList.size() > 0){
            depts = deptList.stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
            depts = depts.stream().distinct().collect(Collectors.toList());
        }
        // 权限集合
        List<String> permissions = new LinkedList<>();
        // 管理员拥有所有权限
        if (sysUser.isAdmin()){
            permissions.add("*:*:*");
        }else {
            if(depts.size() > 0){
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (Long deptId : depts)
                {
                    Set<String> rolePerms = menuService.selectMenuPermsByDeptId(deptId);
                    permissions.addAll(rolePerms);
                }
            }
        }
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSysUser(sysUser);
        sysUserVo.setDepts(depts);
        sysUserVo.setPermissions(permissions);
        return R.ok(sysUserVo);
    }

    @Override
    public R<Boolean> registerUserInfo(SysUser sysUser, String source) {
        return null;
    }

    @Override
    public int getUserMenuCount(Long userId, String menuPerm) {
        if(1L == userId){
            return 1;
        }
        //先查询是否有个人权限，如果有个人权限则以个人权限为主
        List<String> userMenu = menuService.selectPerms(userId);
        if(userMenu.contains(menuPerm)){
            return 1;
        }else{
            // 岗位集合
            List<SysUserDept> deptList = userDeptService.getBaseMapper().selectList(
                    new LambdaQueryWrapper<SysUserDept>()
                            .eq(SysUserDept::getUserId,userId)
            );
            List<Long> depts = new LinkedList<>();
            if(deptList.size() > 0){
                depts = deptList.stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
                depts = depts.stream().distinct().collect(Collectors.toList());
            }
            if(depts.size() > 0){
                for (Long deptId : depts)
                {
                    Set<String> rolePerms = menuService.selectMenuPermsByDeptId(deptId);
                    if(rolePerms.contains(menuPerm)){
                        return 1;
                    }
                }
            }
        }
        return 0;
    }

    @Override
    public List<SysUser> getUserListInfo() {
        return userMapper.getUserInfo();
    }

    @Override
    public SysUser getUserByName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }

    @Override
    public List<SysUser> listByIds(String userIds) {
        String[] ids = userIds.split(",");
        List<Long> userId = Arrays.stream(ids).map(s->Long.parseLong(s)).collect(Collectors.toList());
        List<SysUser> sysUsers =  userMapper.listByIds(userId);
        return sysUsers;
    }
}
